﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//namespace AnonymousTypeInitializer
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            List<Person> people = new List<Person>
//            {
//                new Person { ID = 1, Name = "Paul", Email = "pkimmel@softconcepts.com" },
//                new Person { ID = 2, Name = "Joe", Email = "jswan@gmail.com" }
//            };

//            var names = from p in people select new { FirstName = p.Name };
//            foreach (var name in names)
//            {
//                Console.WriteLine(name.FirstName);
//            }
//            Console.ReadLine();
//        }
//    }

//    public class Person
//    {
//        private int id;
//        public int ID
//        {
//            get { return id; }
//            set { id = value; }
//        }

//        private string name;
//        public string Name
//        {
//            get { return name; }
//            set { name = value; }
//        }

//        private string email;
//        public string Email
//        {
//            get { return email; }
//            set { email = value; }
//        }
//    }
//}

//namespace OfTypeDemo
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            var numbers = new object[] { 1, "two", null, "3", 4 };
//            foreach (var anInt in numbers.OfType<int>())
//                Console.WriteLine(anInt);
//            Console.ReadLine();
//        }
//    }
//}

//namespace CastDemo
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            var numbers = new object[] { 1, "two", null, "3", 4 };
//            foreach (var anInt in numbers.Cast<int>())
//                Console.WriteLine(anInt);
//            Console.ReadLine();
//        }
//    }
//}

//namespace AsEnumerable
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            const string Bacon = "For it is the solecism of a Prince to think to control the end yet not endure the mean.";
//            MyList<string> strings = new MyList<string>();
//            strings.AddRange(Bacon.Split());
//            IEnumerable<string> inMylist = strings.Where(str => str == "solecism");
//            foreach (string s in inMylist)
//                Console.WriteLine(s);

//            Console.ReadLine();

//            IEnumerable<string> notInMyList = strings.AsEnumerable().Where(str => str == "Prince");

//            foreach (string s in notInMyList)
//                Console.WriteLine(s);

//            Console.ReadLine();
//        }
//    }

//    public class MyList<T> : List<T>
//    {
//        public IEnumerable<T> Where(Func<T, bool> predicate)
//        {
//            Console.WriteLine("MyList");
//            return Enumerable.Where(this, predicate);
//        }
//    }
//}

//namespace ToListDemo
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            var teams = new string[] { "Spartans", "Fighting Irish", "Wolverines" };
//            List<string> footballTeams = (from name in teams select name).ToList<string>();
//            footballTeams.Add("Hoosiers");
//            footballTeams.Add("Fighting Illini");
//            footballTeams.Add("Badger");
//            Console.WriteLine(footballTeams.Count);
//            Console.ReadLine();
//        }
//    }
//}

//namespace ToDictionaryDemo
//{
//    class Program
//    {
//        public class Game
//        {
//            public string Opponent { get; set; }
//            public string Score { get; set; }
//        }

//        static void Main(string[] args)
//        {
//            var spartans = new List<Game>
//            {
//                new Game { Opponent = "UAB", Score = "55 - 18"},
//                new Game { Opponent = "Bowling Green", Score = "28 - 17"},
//                new Game { Opponent = "Pittsburgh", Score = "17 - 13" },
//                new Game { Opponent = "Notre Dame", Score = "17 - 14" }
//            };

//            //var games = from g in spartans select g;

//            IDictionary<string, Game> stats = spartans.ToDictionary(key => key.Opponent);
//            Console.WriteLine("Spartans vs. {0} {1}", stats["Notre Dame"].Opponent, stats["Notre Dame"].Score);
//            Console.ReadLine();
//        }
//    }
//}

namespace ToLookupDemo
{
    class Program
    {
        public class Product
        {
            public string Code { get; set; }
            public string Description { get; set; }
        }

        static void Main(string[] args)
        {
            List<Product> products = new List<Product>
            {
                new Product { Code = "SPM1", Description = "Spam"},
                new Product { Code = "SPM2", Description = "Mechanically Separated Chicken"},
                new Product { Code = "LME1", Description = "Bologna"},
                new Product { Code = "SUP1", Description = "Tomato"},
                new Product { Code = "SUP2", Description = "Chichen Noodle"}
            };

            Lookup<string, string> lookup = (Lookup<string, string>)products.ToLookup(c => c.Code.Substring(0, 3), c => c.Code + " " + c.Description);

            foreach (IGrouping<string, string> group in lookup)
            {
                Console.WriteLine(group.Key);
                foreach (string s in group)
                    Console.WriteLine(" {0}", s);
            }

            Console.ReadLine();

            IEnumerable<string> spmGroup = lookup["SPM"];
            foreach (var str in spmGroup)
                Console.WriteLine(str);

            Console.ReadLine();
        }
    }
}
